<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/3 16:07
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

/**
 * 文本化时间戳
 * @param $timestamp
 * @return bool|string
 */
function formateTimeStamp($timestamp)
{

    if (!is_numeric($timestamp)) {
        $timestamp = strtotime($timestamp);
    }

    $date = $now = $tmp = array();
    $timeString = date("Y/n/j/G/i/s", $timestamp);

    $tmp = explode('/', $timeString);
    $date['year'] = $tmp[0];
    $date['month'] = $tmp[1];
    $date['day'] = $tmp[2];
    $date['hour'] = $tmp[3];
    $date['minute'] = $tmp[4];
    $date['second'] = $tmp[5];

    $tmp = explode('/', date("Y/n/j/G/i/s"));
    $now['year'] = $tmp[0];
    $now['month'] = $tmp[1];
    $now['day'] = $tmp[2];
    $now['hour'] = $tmp[3];
    $now['minute'] = $tmp[4];
    $now['second'] = $tmp[5];

    if ($date['year'] < $now['year']) {
        return date("Y-m-d", $timestamp);
    }

    if ($date['month'] < $now['month'] || ($date['day'] + 2) < $now['day']) {
        return date("m-d", $timestamp);
    }

    if ($now['day'] - $date['day'] == 2) {
        return date('前天 H:i', $timestamp);
    }

    if ($now['day'] - $date['day'] == 1) {
        return date('昨天 H:i', $timestamp);
    }

    if ($date['hour'] <= 12 && $date['hour'] > 6) {
        return date('上午 H:i', $timestamp);
    }

    if ($date['hour'] <= 6) {
        return date('凌晨 h:i', $timestamp);
    }

    return date('下午 h:i', $timestamp);
}
